import requests
def get_hash_list(keyword):
    url='http://mobilecdn.kugou.com/api/v3/search/song'
    params={
        "format": "json",
        "keyword": keyword,
        "page": "1",
        "pagesize": "30",
        "showtype": "1",
        "callback": "",
    }
    response= requests.get(url,params=params)
    json_data=response.json()
    return json_data['data']['info']

def get_play_url(keyword):
    hash_json=get_hash_list(keyword)
    url="http://m.kugou.com/api/v1/song/get_song_info"
    params={
        "cmd": "playInfo",
        "hash": "21638ab9b1bb9ad1847f6f52ef62884e",
        "from": "mkugou",
        "apiver": "2",
        "mid": "a467c21962ba706efbcda52d28c5f7b9",
        "userid": "0",
        "platid": "4",
        "dfid": "2lPEz12R9tVX16HH1h0Diyp2"
    }
    headers={
        "Host": "m.kugou.com",
        "Referer": "http://m.kugou.com/search/index",
        "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Mobile Safari/537.36 Edg/87.0.664.52"
    }
    for i in hash_json:
        _hash=i['hash']
        params['hash']=_hash
        response=requests.get(url,params=params,headers=headers)
        json_data=response.json()
        try:
            if json_data['url']!='':
                return {'name':json_data['fileName'],"url":json_data['url']}
        except Exception:
            continue
print(get_play_url("海阔天空"))

